package org.jsola.hr.web;

import org.jsola.core.Result;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.service.IQRCodeService;
import org.jsola.hr.vo.DynamicCodeVO;
import org.jsola.hr.vo.QRCodeVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 小程序
 */
@RestController("hrMiniProgramController")
@RequestMapping(HrConstants.API_PATH + "/v1/miniProgram")
public class MiniProgramController implements IMiniProgramControllerAPI {

    @Autowired
    private IQRCodeService codeService;


    @Override
    @GetMapping("/getQRCode")
    public Result<QRCodeVO> getQRCode(@RequestParam("companyId") String companyId) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                codeService.getQRCode(companyId, tokenUser)
        );
    }

    @Override
    @PostMapping("/generateQRCode")
    public Result<QRCodeVO> generateQRCode(@RequestBody @Valid QRCodeVO qrCodeVO) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                codeService.generateQRCode(qrCodeVO, tokenUser)
        );
    }

    @Override
    @GetMapping("/getByKey")
    public Result<QRCodeVO> getByKey(@RequestParam("key") String key) {
        return Result.success(
                codeService.getByKey(key)
        );
    }

    @Override
    @GetMapping("/getQRCodeByCompanyId")
    public Result<QRCodeVO> getQRCodeByCompanyId(@RequestParam() String companyId) {

        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                codeService.getQRCodeByCompanyId(companyId, tokenUser)
        );
    }

    @Override
    @GetMapping("/getManageQRCode")
    public Result<QRCodeVO> getManageQRCode(@RequestParam("companyId") String companyId,
                                            String channelId,
                                            @RequestParam("type") Integer type) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                codeService.getManageQRCode(companyId, channelId, type, tokenUser)
        );
    }

    @Override
    @GetMapping("/getDynamicCode")
    public Result<DynamicCodeVO> getDynamicCode(@RequestParam("companyId") String companyId,
                                                String channelId,
                                                String oldCode,
                                                @RequestParam("type") Integer type) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                codeService.getDynamicCode(companyId, channelId, oldCode, type, tokenUser)
        );
    }

    @Override
    @GetMapping("/getByDynamicCode")
    public Result<DynamicCodeVO> getByDynamicCode(@RequestParam String code) {

        return Result.success(
                codeService.getByDynamicCode(code)
        );
    }

    @Override
    @GetMapping("/generateSalaryQRCode")
    public Result<String> generateSalaryQRCode(String batchId, Integer batchType, String siteId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                codeService.generateSalaryQRCode(batchId, batchType, siteId, tokenUser)
        );
    }
}
